import router from '../router'
import { ifDefaultRouter } from '@/hooks/useAppRoute'
import NProgress from '@/utils/system/nprogress'
import userStoreCreater from '../store/userStore'

// 前置守卫
router.beforeEach((to, from, next) => {
  NProgress.start()
  const userStore = userStoreCreater()
  if (ifDefaultRouter(to)) {
    next()
  } else {
    // 验证登陆
    if (userStore.isLogin) {
      if (to.name != 'xzqh' && userStore.orgnow.orgnowcode == '') {
        next({ name: 'xzqh' })
      } else {
        next()
      }
    } else {
      // 没登录
      console.error('1.请检查token', userStore.token)
      console.error('2.请检查路由', to.path)
      next({ name: 'login', query: { redirect: to.path } })
    }
  }
})

// 后置守卫
router.afterEach((to, from, next) => {
  NProgress.done()
})
